package com.shuwang.order.controller;

import com.shuwang.order.domian.UserChoiceGoodsDomian;
import com.shuwang.order.exception.NotFoundException;
import com.shuwang.order.mapper.GoodsSkuMapper;
import com.shuwang.order.mapper.OrderInfoMapper;
import com.shuwang.order.mapper.ShopInfoMapper;
import com.shuwang.order.model.GoodsSku;
import com.shuwang.order.model.OrderInfo;
import com.shuwang.order.model.OrderInfoExample;
import com.shuwang.order.until.IDBuilder;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by think on 2017/6/5.
 */
@RestController
@RequestMapping(value = "order")
@Api(value = "info",description = "用户订单 API")
public class OrderInfoController {
    //打印日志信息
    protected final Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private ShopInfoMapper shopInfoMapper;
    @RequestMapping(value = "info/order/{shop_id}/{user_id}/{sum_price}/{goods}/{address_id}", method = RequestMethod.POST)
    @ApiOperation(value = "用户选择商品接口",response =OrderInfo.class ,
            notes = "用户在一家商户里选择购物，goods格式    商品ID1，购买数量;商品ID2，购买数量;" +
                    "地址为地址ID"
                    )

    public UserChoiceGoodsDomian postOrderController(
            @ApiParam(value = "商户Id") @PathVariable Long shop_id,
            @ApiParam(value = "用户Id") @PathVariable Long user_id,
            @ApiParam(value = "支付金额") @PathVariable Long sum_price,
            @ApiParam(value = "商品") @PathVariable String goods,
            @ApiParam(value = "地址Id") @PathVariable Long address_id
    )
    {   Date today = new Date();
        OrderInfo orderInfo=new OrderInfo();
        Long getId = IDBuilder.CreateAnyId("219");
        orderInfo.setOrderId(getId);
        orderInfo.setCreateTime(today);
        orderInfo.setUserId(user_id);
        orderInfo.setGoods(goods);
        orderInfo.setShopId(shop_id);
        orderInfo.setShopName(shopInfoMapper.selectByPrimaryKey(shop_id).getShopName());
        orderInfo.setAddressId(address_id);
        orderInfo.setSumPrice(sum_price);
        orderInfo.setStatus(2201);
        orderInfoMapper.insert(orderInfo);
        UserChoiceGoodsDomian userChoiceGoodsDomian=new UserChoiceGoodsDomian();
        BeanUtils.copyProperties(orderInfo,userChoiceGoodsDomian);
        return  userChoiceGoodsDomian;}

    @RequestMapping(value = "info/pay/{order_id}/{choice}", method = RequestMethod.PUT)
    @ApiOperation(value = "用户支付接口",response =OrderInfo.class ,
            notes = "支付状态输入任意值视为支付"
    )

    public String UserPayController(
            @ApiParam(value = "订单ID") @PathVariable Long order_id,
            @ApiParam(value = "支付状态") @PathVariable String choice
    )
    {   OrderInfo orderInfo=orderInfoMapper.selectByPrimaryKey(order_id);

        if (orderInfo==null)
    {
        throw new NotFoundException("未找到任何相关订单信息，请确认订单ID");
    }
        String request=new String();
        if (choice!=null)
        {request="您的订单支付成功";
            orderInfo.setStatus(2202);
            Date today = new Date();
            orderInfo.setPayTime(today);
        orderInfoMapper.updateByPrimaryKeySelective(orderInfo);}
        else {request="您的订单支付失败";}
        return  request;}



    @RequestMapping(value = "info/send/{order_id}/{choice}", method = RequestMethod.PUT)
    @ApiOperation(value = "商家发货接口",response =OrderInfo.class ,
            notes = "发货状态输入任意值视为发货"
    )
    public String ShopSendController(
            @ApiParam(value = "订单ID") @PathVariable Long order_id,
            @ApiParam(value = "发货状态") @PathVariable String choice
    )
    {   OrderInfo orderInfo=orderInfoMapper.selectByPrimaryKey(order_id);

        if (orderInfo==null)
        {
            throw new NotFoundException("未找到任何相关订单信息，请确认订单ID");
        }
        String request=new String();
        if (choice!=null)
        {request="您的订单已成功发货";
            orderInfo.setStatus(2203);
            orderInfoMapper.updateByPrimaryKeySelective(orderInfo);}
        else {request="您的订单还未发货";}
        return  request;}





    @RequestMapping(value = "info/receive/{order_id}/{choice}", method = RequestMethod.PUT)
    @ApiOperation(value = "用户收货接口",
//            response =OrderInfo.class ,
            notes = "收货状态输入任意值视为收货"
    )
    public String UserReceiveController(
            @ApiParam(value = "订单ID") @PathVariable Long order_id,
            @ApiParam(value = "收货状态") @PathVariable String choice
    )
    {
        OrderInfo orderInfo=orderInfoMapper.selectByPrimaryKey(order_id);

        if (orderInfo==null)
        {
            throw new NotFoundException("未找到任何相关订单信息，请确认订单ID");
        }
        String request=new String();
        if (choice!=null)
        {request="您已成功收货";
            orderInfo.setStatus(2204);
            orderInfoMapper.updateByPrimaryKeySelective(orderInfo);}
       else{ request="您的订单仍在派送中";}
        return  request;
    }

    @RequestMapping(value = "info/All/{user_id}", method = RequestMethod.GET)
    @ApiOperation(value = "用户查询所有接口",
//            response =OrderInfo.class ,
            notes = "根据用户id返回所有的订单ID"
    )
    public List<Long> UserViewController(
            @ApiParam(value = "用户ID") @PathVariable Long user_id
    ) {
        OrderInfoExample example = new OrderInfoExample();
        example.createCriteria().andUserIdEqualTo(user_id);
        List<OrderInfo> orderInfo = orderInfoMapper.selectByExample(example);
        if (orderInfo == null) {
            throw new NotFoundException("你还没有任何相关订单信息");
        }
        List<Long> order_id = null;
        order_id = new ArrayList<>();
        for (int i = 0; i < orderInfo.size(); i++) {
            order_id.add(orderInfo.get(i).getOrderId());
        }
        return order_id;
    }

    @RequestMapping(value = "info/Order_id/{order_id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据订单ID查询该订单的所有信息"

    )
    public OrderInfo UserViewByOrderIdController(
            @ApiParam(value = "订单ID") @PathVariable Long order_id
    ) {

        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(order_id);
        if (orderInfo == null) {
            throw new NotFoundException("未找到任何相关订单信息，请确认订单ID");
        }

        return orderInfo;
    }
}
